package com.humble.vichatbackend.web_socket;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ChatSocketUtil {
    public static boolean joinChat(Long userId,Long teamId){
        if (ChatSocketManager.CHAT_SOCKET_MAP.containsKey(teamId)){
            ChatSocket chatSocket = ChatSocketManager.CHAT_SOCKET_MAP.get(teamId);
            chatSocket.getTEAM_USERID().add(userId);
            return true;
        }
        else{
            log.info("找不到需要加入的聊天室，聊天室teamID:{}",teamId);
            return false;
        }
    }

    public static boolean removeFromChat(Long userId,Long teamId){
        if (ChatSocketManager.CHAT_SOCKET_MAP.containsKey(teamId)){
            ChatSocket chatSocket = ChatSocketManager.CHAT_SOCKET_MAP.get(teamId);
            chatSocket.getTEAM_USERID().remove(userId);
            chatSocket.getONLINE_USER_MAP().remove(userId);
            chatSocket.getLEAVE_MESSAGE_MAP().remove(userId);
            return true;
        }
        else{
            log.info("找不到需要加入的聊天室，聊天室teamID:{}",teamId);
            return false;
        }
    }

    public static boolean createChat(Long userId,Long teamId){
        try {
            ChatSocket chatSocket = new ChatSocket();
            chatSocket.getTEAM_USERID().add(userId);
            ChatSocketManager.CHAT_SOCKET_MAP.put(teamId,chatSocket);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return false;
        }
    }

}
